﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using Domain;
using Domain.Entities;

namespace Web.ViewModels
{
    public class OrderViewModel
    {
        public Order order { get; set; }
        public List<ProduceModel>  orderDetails { get; set; }
    }

    public class EditOrderDetailViewModel
    {
        public int OrderId { get; set; }
        public List<OrderDetailViewModel> list { get; set; }

    }


    public class OrderDetailViewModel
    {
        public int Id { get; set; }
        public int Quantity { get; set; }
        public double UnitPrice { get; set; }


        public string Category { get; set; }
        public string Producer { get; set; }
        public string Package { get; set; }
    }

    public class ProduceCatalog
    {

        EFDbContext context = new EFDbContext(ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString);
        public int OrderId { get; set; }
        public int OrderDetailId { get; set; }

  
       
        [Required(ErrorMessage = "*")]
        [Range(1, int.MaxValue)]
        public int Count { get; set; }

        [Required(ErrorMessage = "*")]
        public int? SelectedCategoryId { get; set; }
        [Required(ErrorMessage = "*")]
        public int? SelectedProduceId { get; set; }
        [Required(ErrorMessage = "*")]
        public int? SelectedPrepackageId { get; set; }

        public SelectList GetListCategory()
        {
            IEnumerable<Category> catList = context.Categories.ToList();
            var list = catList.Select(m => new SelectListItem() {Text = m.Name, Value = m.Id.ToString()}).ToList();
            return new SelectList(list, "Value", "Text", SelectedCategoryId);
        }

        public SelectList GetListProduce()
        {
            var plist =
                context.ProduceInPackages.Select(p => p.Produce)
                    .Where(i => i.CategoryId == SelectedCategoryId)
                    .OrderBy(n => n.Name)
                    .Distinct()
                    .ToList();

            var list = plist.Select(m => new SelectListItem() {Text = m.Name, Value = m.Id.ToString()});

            return new SelectList(list, "Value", "Text", SelectedProduceId);
        }

        public SelectList GetListPrepackage()
        {
            var plist =
                context.ProduceInPackages
                    .Where(i => i.ProduceId == SelectedProduceId && i.Produce.CategoryId==SelectedCategoryId)
                    .Select(p => p.Prepackage)
                    .OrderBy(n => n.Weight)
                    .Distinct()
                    .ToList();

            var list = plist.Select(m => new SelectListItem() { Text = m.Weight.ToString(), Value = m.Id.ToString() });
            return new SelectList(list, "Value", "Text", SelectedProduceId);
        }

        

    }



}